package com.py.framework.service.impl;

import java.sql.Timestamp;
import java.util.List;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.framework.common.Constants;
import com.py.framework.dao.WorkFlowDao;
import com.py.framework.entity.WorkFlowBus;
import com.py.framework.service.WorkFlowService;

/**
 * 工作流Service实现
 *
 * @author Leegern
 * @date 2018年5月24日
 */
@Service
public class WorkFlowServiceImpl implements WorkFlowService {

    @Autowired
    private WorkFlowDao workflowDao;
    @Autowired
    private RuntimeService runtimeService;

    /*
     * (non-Javadoc)
     *
     * @see
     * com.py.framework.service.WorkflowService#getActsByName(java.lang.String)
     */
    @Override
    public int getActsByName(String actName) {
        if (StringUtils.isNotBlank(actName)) {
            return workflowDao.getActsByName(actName);
        }
        return 0;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.py.framework.service.WorkflowService#findActByProcId(java.lang.
     * String)
     */
    @Override
    public WorkFlowBus findActByProcId(String procId) {
        if (StringUtils.isNotBlank(procId)) {
            List<WorkFlowBus> datas = workflowDao.findActByProcId(procId);
            if (!CollectionUtils.isEmpty(datas)) {
                return datas.get(Constants.NUMBER_ZERO);
            }
        }
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.py.framework.service.WorkflowService#updateWorkFlowBus(com.py.
     * framework.entity.WorkFlowBus)
     */
    @Override
    public int updateWorkFlowBus(WorkFlowBus workflow) {
        if (null != workflow) {
            workflow.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            return workflowDao.updateWorkFlowBus(workflow);
        }
        return 0;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.py.framework.service.WorkflowService#addWorkFlowBus(com.py.framework.
     * entity.WorkFlowBus)
     */
    @Override
    public int addWorkFlowBus(WorkFlowBus entity) {
        if (null != entity) {
            entity.setActStatus(Constants.NUMBER_ONE);
            entity.setProStatus(Constants.NUMBER_ONE);
            entity.setDealStatus(Constants.NUMBER_ONE);
            entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
            entity.setDelFlag(Constants.NUMBER_ONE);
            return workflowDao.addWorkFlowBus(entity);
        }
        return 0;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.py.framework.service.WorkflowService#startWorkflow()
     */
    @Override
    public void startWorkflow() {
        List<WorkFlowBus> jobs = workflowDao.getNoStartWFJobs();
        WorkFlowBus entity = null;
        if (!CollectionUtils.isEmpty(jobs)) {
            for (WorkFlowBus workFlowBus : jobs) {
                if (null != workFlowBus && StringUtils.isNotBlank(workFlowBus.getWfKey())) {
                    // 启动流程
                    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(workFlowBus.getWfKey());
                    String procId = processInstance.getProcessInstanceId();

                    entity = new WorkFlowBus();
                    entity.setId(workFlowBus.getId());
                    entity.setProcId(procId);
                    // 处理状态为已处理
                    entity.setDealStatus(Constants.NUMBER_TWO);
                    // 流程状态:审核
                    entity.setProStatus(Constants.NUMBER_TWO);
                    this.updateWorkFlowBus(entity);
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see com.py.framework.service.WorkFlowService#getActByWFId(long)
     */
    @Override
    public WorkFlowBus getActByWFId(long wfId) {
        if (wfId > Constants.NUMBER_ZERO) {
            List<WorkFlowBus> datas = workflowDao.getActByWFId(wfId);
            if (!CollectionUtils.isEmpty(datas)) {
                return datas.get(0);
            }
        }
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.py.framework.service.WorkFlowService#getActsByBusId(long)
     */
    @Override
    public List<WorkFlowBus> getActsByBusId(long busId) {
        if (busId > Constants.NUMBER_ZERO) {
            return workflowDao.getActsByBusId(busId);
        }
        return null;
    }

    @Override
    public List<WorkFlowBus> getHandledWorkFlowBus() {

        return workflowDao.selectHandledWorkFlowBus();

    }

    @Override
    public void updateWorkFlowUpdateFlag(String id) {
        workflowDao.updateWorkFlowUpdateFlag(id);

    }

    @Override
    public int getDataOwnerByTaskId(String taskId) {
        return workflowDao.selectDataOwnerByTaskId(taskId);
    }

    @Override
    public int getBusIdByTaskId(String taskId) {
        return workflowDao.selectBusIdByTaskId(taskId);
    }

    @Override
    public WorkFlowBus getDissentProcessHistory(String key, String id) {
        WorkFlowBus workFlowBus = workflowDao.getDissentProcessHistory(key, id);
        return workFlowBus;
    }
}